package com.qd.pandaparkflow.compment.park;

import com.cdqidi.exception.ApiException;
import com.qd.common.panda.domain.entity.rule.*;
import com.qd.common.panda.domain.enums.CarParkBillRuleEnum;
import com.qd.pandaparkflow.bean.PriceReqVO;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.util.List;
import java.util.Objects;

/**
 * 参数验证组件
 *
 * @author sjk
 */
@ConditionalOnProperty(prefix = "liteflow", name = "enable", havingValue = "true")
@LiteflowComponent(value = "checkCmp")
@Slf4j
public class CheckComponent extends NodeComponent {

    @Override
    public void process() throws Exception {
        PriceReqVO priceReqVO = this.getSlot().getRequestData();
        if (null == priceReqVO.getStartTime() || null == priceReqVO.getEndTime()) {
            throw new ApiException("开始时间和结束时间不能为空");
        }
        if (priceReqVO.getEndTime().isBefore(priceReqVO.getStartTime())) {
            throw new ApiException("结束时间不能小于开始时间");
        }
        //验证计费规则ID是否存在，不存在直接报错
        CarParkBillRuleEnum.checkCode(priceReqVO.getBillRule());
        final List<BaseRuleDTO> rules = priceReqVO.getRules();
        if (null == rules || rules.isEmpty()) {
            throw new ApiException("规则内容不能为空");
        }
        final BaseRuleDTO baseRuleDTO = rules.get(0);
        boolean isParamCountError = false;
        switch (Objects.requireNonNull(CarParkBillRuleEnum.getByValue(priceReqVO.getBillRule()))) {
            case BILL_RULE1:
                if (rules.size() != 1) {
                    isParamCountError = true;
                }
                if (!(baseRuleDTO instanceof Rule1DTO)) {
                    isParamCountError = true;
                }
                break;
            case BILL_RULE2:
                if (rules.size() != 1) {
                    isParamCountError = true;
                }
                if (!(baseRuleDTO instanceof Rule2DTO)) {
                    isParamCountError = true;
                }
                break;
            case BILL_RULE3:
                if (rules.size() != 1) {
                    isParamCountError = true;
                }
                if (!(baseRuleDTO instanceof Rule3DTO)) {
                    isParamCountError = true;
                }
                break;
            case BILL_RULE4:
                if (rules.size() < 2) {
                    isParamCountError = true;
                }
                if (!(baseRuleDTO instanceof Rule4DTO)) {
                    isParamCountError = true;
                }
                break;
            case BILL_RULE5:
                if (rules.size() < 2) {
                    isParamCountError = true;
                }
                if (!(baseRuleDTO instanceof Rule5DTO)) {
                    isParamCountError = true;
                }
                break;
            case BILL_RULE6:
                if (!(baseRuleDTO instanceof Rule6DTO)) {
                    isParamCountError = true;
                }
                break;
            default:
                log.error("不支持的规则:{}", priceReqVO.getBillRule());
                throw new ApiException("不支持的规则");
        }
        if (isParamCountError) {
            log.error("billRule: {},rule:{}", priceReqVO.getBillRule(), rules);
            throw new ApiException("规则内容不正确");
        }
    }
}